//
//  RealtimeMeasureVc.m
//  CareRingApp
//
//  Created by Linktop on 2023/3/31.
//

#import <Masonry/Masonry.h>
#import "RealtimeMeasureVc.h"
#import <QMUIKit/QMUIKit.h>
#import "ConfigModel.h"
#import "LTSRingSDK.h"
#import "DeviceCenter.h"
#import "SRDeviceInfo+description.h"
#import "UIViewController+Custom.h"

NSString * const prif_temp = @"Skin Temperature: ";
NSString * const prif_hr = @"Heart Rate: ";
NSString * const prif_spo = @"Spo2: ";
NSString * const prif_hrv = @"HRV: ";
NSString * const prif_ir = @"IR data: ";
NSString * const prif_red = @"Red data: ";

@interface RealtimeMeasureVc ()<SRBleScanProtocal, SRBleDataProtocal>

@property(strong, nonatomic)QMUIButton *startBtn;
@property(strong, nonatomic)UISegmentedControl *seg;
@property(strong, nonatomic)UILabel *hrLbl, *spoLbl, *skinTemperatureLbl, *hrvLbl, *irLbl, *redLbl;
@property(strong, nonatomic)UISwitch *swh;

@end

@implementation RealtimeMeasureVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Realtime Measurement";
    [DeviceCenter instance].sRappScanDelegate = self;
    [DeviceCenter instance].appDataDelegate = self;

    
    WEAK_SELF
    [self arrowback:^{
        
        
        STRONG_SELF
        [strongSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [self initUI];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.swh.enabled = [DeviceCenter instance].isBleConnected;
    self.swh.on = [DeviceCenter instance].currentDevice.isWaveDataOn;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    // stop measurement
    
    if (self.startBtn.isSelected) { // stop meaaure
        self.seg.enabled = YES;
        if (self.seg.selectedSegmentIndex == 0) {
            [[DeviceCenter instance].sdk functionOxygenOrHeartRate:REALTIME_MEASURE_HR IsOpen:NO];
        }
        
        if (self.seg.selectedSegmentIndex == 1) {
            [[DeviceCenter instance].sdk functionOxygenOrHeartRate:REALTIME_MEASURE_SPO IsOpen:NO];
        }
    }
    
}


-(void)initUI
{
    [self.view addSubview:self.seg];
    UIView *switchCont = [UIView new];
    [self.view addSubview:switchCont];
    
    [switchCont mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(20);
        make.height.equalTo(@44);
    }];
    [self layoutSwitchCnt:switchCont];
    
    [self.seg mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(switchCont.mas_bottom).offset(10);
        make.height.equalTo(@44);
        make.width.equalTo(@180);
    }];
    
    [self.view addSubview:self.skinTemperatureLbl];
    [self.view addSubview:self.hrLbl];
    [self.view addSubview:self.spoLbl];
    [self.view addSubview:self.hrvLbl];
    [self.view addSubview:self.irLbl];
    [self.view addSubview:self.redLbl];

    [self.skinTemperatureLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.seg.mas_bottom).offset(30);
        make.centerX.equalTo(self.seg.mas_centerX);
        make.height.equalTo(@40);
        make.width.equalTo(self.view.mas_width);
    }];
    
    [self.hrLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.skinTemperatureLbl.mas_bottom).offset(5);
        make.centerX.equalTo(self.seg.mas_centerX);
        make.height.equalTo(@40);
        make.width.equalTo(self.view.mas_width);
    }];
    
    [self.spoLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hrLbl.mas_bottom).offset(5);
        make.centerX.equalTo(self.seg.mas_centerX);
        make.height.equalTo(@40);
        make.width.equalTo(self.view.mas_width);
    }];
    
    [self.hrvLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.spoLbl.mas_bottom).offset(5);
        make.centerX.equalTo(self.seg.mas_centerX);
        make.height.equalTo(@40);
        make.width.equalTo(self.view.mas_width);
    }];
    
    [self.irLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hrvLbl.mas_bottom).offset(5);
        make.centerX.equalTo(self.seg.mas_centerX);
        make.height.equalTo(@40);
        make.width.equalTo(self.view.mas_width);
    }];
    [self.redLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.irLbl.mas_bottom).offset(5);
        make.centerX.equalTo(self.seg.mas_centerX);
        make.height.equalTo(@40);
        make.width.equalTo(self.view.mas_width);
    }];
    
    [self.view addSubview:self.startBtn];
    
    [self.startBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.leading.equalTo(self.view.mas_leading).offset(30);
        make.trailing.equalTo(self.view.mas_trailing).inset(30);
        make.height.equalTo(@40);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).inset(50);
    }];
    
    [self cleanText];
    
}

-(void)layoutSwitchCnt:(UIView *)cnt {
    
    UILabel *title = [UILabel new];
    title.text = @"Wave Data On/Off :";
    [cnt addSubview:title];
    
    UISwitch *swh = [UISwitch new];
    [cnt addSubview:swh];

    self.swh = swh;
    
    [title mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(cnt.mas_leading).offset(5);
        make.centerY.equalTo(cnt.mas_centerY);
        
    }];
    
    [self.swh mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(cnt).inset(5);
        make.centerY.equalTo(cnt.mas_centerY);
    }];
    [self.swh addTarget:self action:@selector(turnWaveSwitch:) forControlEvents:UIControlEventValueChanged];
    
}

-(void)turnWaveSwitch:(UISwitch *)swh {
    
    [[DeviceCenter instance].sdk functionSetRealmeasureWaveSwitch:swh.isOn];
    
}

-(void)startBtnClick:(QMUIButton *)btn {
    
    if (!btn.isSelected) { // start meaaure
        self.seg.enabled = NO;
        [self cleanText];
        if (self.seg.selectedSegmentIndex == 0) {
            [[DeviceCenter instance].sdk functionOxygenOrHeartRate:REALTIME_MEASURE_HR IsOpen:YES];
        }
        
        if (self.seg.selectedSegmentIndex == 1) {
            [[DeviceCenter instance].sdk functionOxygenOrHeartRate:REALTIME_MEASURE_SPO IsOpen:YES];
        }
        [[DeviceCenter instance].sdk functionGetTemperatureOnce];
        
        
    }
    
    if (btn.isSelected) { // stop meaaure
        self.seg.enabled = YES;
        if (self.seg.selectedSegmentIndex == 0) {
            [[DeviceCenter instance].sdk functionOxygenOrHeartRate:REALTIME_MEASURE_HR IsOpen:NO];
        }
        
        if (self.seg.selectedSegmentIndex == 1) {
            [[DeviceCenter instance].sdk functionOxygenOrHeartRate:REALTIME_MEASURE_SPO IsOpen:NO];
        }
    }
    
    
    btn.selected = !btn.isSelected;
}


-(void)cleanText {
    
    self.hrLbl.text = prif_hr;
    self.skinTemperatureLbl.text = prif_temp;
    self.spoLbl.text = prif_spo;
    
}

-(QMUIButton *)startBtn
{
    if (!_startBtn) {
        _startBtn = [[QMUIButton alloc]init];
        _startBtn.backgroundColor = MAIN_BLUE;
        _startBtn.cornerRadius = ITEM_CORNOR_RADIUS;
        [_startBtn setTitle:@"Start" forState:UIControlStateNormal];
        
        [_startBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_startBtn setTitleColor:UIColor.redColor forState:UIControlStateSelected];

        [_startBtn setTitle:@"Stop" forState:UIControlStateSelected];
        [_startBtn addTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _startBtn;
}

-(void)segSelect:(UISegmentedControl *)seg {
    
}

-(UISegmentedControl *)seg
{
    if (!_seg) {
        NSArray<NSString *> *titles =@[@"Heart Rate", @"SPO2"];

        _seg = [[UISegmentedControl alloc]initWithItems:titles];
        _seg.selectedSegmentIndex = 0; // default
        [_seg addTarget:self action:@selector(segSelect:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _seg;
}

-(UILabel *)hrLbl
{
    if (!_hrLbl) {
        _hrLbl = [UILabel new];
        _hrLbl.textAlignment = NSTextAlignmentCenter;
        
    }
    return _hrLbl;
}

-(UILabel *)spoLbl
{
    if (!_spoLbl) {
        _spoLbl = [UILabel new];
        _spoLbl.textAlignment = NSTextAlignmentCenter;
    }
    return _spoLbl;
}

-(UILabel *)hrvLbl
{
    if (!_hrvLbl) {
        _hrvLbl = [UILabel new];
        _hrvLbl.textAlignment = NSTextAlignmentCenter;
    }
    return _hrvLbl;
}

-(UILabel *)irLbl
{
    if (!_irLbl) {
        _irLbl = [UILabel new];
        _irLbl.textAlignment = NSTextAlignmentCenter;
        _irLbl.text = prif_ir;

    }
    return _irLbl;
}
-(UILabel *)redLbl
{
    if (!_redLbl) {
        _redLbl = [UILabel new];
        _redLbl.textAlignment = NSTextAlignmentCenter;
        _redLbl.text = prif_red;
    }
    return _redLbl;
}
-(UILabel *)skinTemperatureLbl
{
    if (!_skinTemperatureLbl) {
        _skinTemperatureLbl = [UILabel new];
        _skinTemperatureLbl.textAlignment = NSTextAlignmentCenter;
    }
    return _skinTemperatureLbl;
}

#pragma mark -- SRBleScanProtocal, SRBleDataProtocal

- (void)srBleCmdExcute:(EXCUTED_CMD)cmd Succ:(BOOL)isSucc Reason:(CMD_EXECTE_ERROR_REASON)reason
{
    if ([self.navigationController topViewController] == self) {
        if (!isSucc && cmd == EXCUTED_CMD_GET_OXYGEN_HRV) {
            NSString *str = [SRDeviceInfo descryOfErrorReason:reason];
            if (str.length) {
                WEAK_SELF
                [self showAlertWarningWithTitle:_L(L_TIPS) Msg:str btnOk:_L(L_OK) OkBLk:^{
                    STRONG_SELF
                } CancelBtn:nil CancelBlk:nil Compelete:nil];
            }
        }
    }
}

- (void)srBleDeviceBatteryLevel:(NSUInteger)batteryLevel IsCharging:(BOOL)isCharging {
    
}

- (void)srBleDeviceDidReadyForReadAndWrite:(nonnull SRBLeService *)service {
    
}

- (void)srBleDeviceInfo:(nonnull SRDeviceInfo *)devInfo {
    
}

- (void)srBleDeviceRealtimeSteps:(nonnull NSNumber *)steps {
    
}

- (void)srBleDeviceRealtimeTemperature:(nonnull NSNumber *)temperature {
    self.skinTemperatureLbl.text = [NSString stringWithFormat:@"%@%.2f%@", prif_temp, temperature.floatValue, _L(L_UNIT_TEMP_C)];

}





- (void)srBleHistoryDataTimeout {
    
}



- (void)srBleIsbinded:(BOOL)isBinded {
    
}

- (void)srBleRealtimeHeartRate:(nonnull NSNumber *)hr {
    self.hrLbl.text = [NSString stringWithFormat:@"%@%d", prif_hr, hr.intValue];
}

- (void)srBleRealtimeSpo:(nonnull NSNumber *)spo {
    self.spoLbl.text = [NSString stringWithFormat:@"%@%d", prif_spo, spo.intValue];

}

- (void)srBleRealtimeHrv:(NSNumber *)hrv
{
    self.hrvLbl.text = [NSString stringWithFormat:@"%@%d", prif_hrv, hrv.intValue];

}

- (void)srBleSN:(nonnull NSString *)sn {
    
    self.swh.on = [DeviceCenter instance].currentDevice.isWaveDataOn;
    
}

// 实时测量红外数据
-(void)srBleIrData:(uint16_t)irData {
    self.irLbl.text = [NSString stringWithFormat:@"%@%d", prif_ir,irData];

}

// 实时测量红光数据
-(void)srBleRedData:(uint16_t)redData
{
    self.redLbl.text = [NSString stringWithFormat:@"%@%d", prif_red, redData];


}



- (void)srBleHistoryDataProgress:(float)percent IsComplete:(BOOL)isComplete {
    
}


- (void)srBleOEMAuthResult:(BOOL)authSucceddful {
    self.swh.enabled = YES;
}




@end
